<?php
/**
 * 系统设置模型管理
 * @author cjli
 *
 */
class SiteModel extends Model
{
	
	/**
	 * 获取语言包目录列表
	 */
	public static function getLanguageDirctoryList()
	{
		$dirArray = array();
		$handle = opendir(WEB_LANG_PATH);
		while (false !== ($file = readdir($handle))){
			if ($file == "." || $file == ".."){
				continue;
			}
			if(is_dir(WEB_LANG_PATH.$file)){
				$dirArray[] = $file;
			}
		}
		return $dirArray;
	}
	
	/**
	 * 获取模板样式
	 */
	public static function getThemsList()
	{
		$dirArray = array();
		$handle = opendir(WEB_TMPL_PATH);
		while (false !== ($file = readdir($handle))){
			if ($file == "." || $file == ".."){
				continue;
			}
			if(is_dir(WEB_TMPL_PATH.$file)){
				$dirArray[] = $file;
			}
		}
		return $dirArray;
	}
	
	/**
	 * 获取语言列表
	 */
	public static function getLanguageList()
	{
		$languageList = array();
		if(S('admin_language_list')){
			$languageList = S('admin_language_list');
		}else{
			$lang_db = D('language');
			$languageArray = $lang_db->field('id,name')->where()->order('listorder DESC')->select();
			if($languageArray){
				foreach ($languageArray as $lang){
					$languageList[$lang['id']] = $lang['name'];
				}
			}
			if(empty($languageList)){
				$languageList['1'] = 'English';
			}
			S('admin_language_list', $languageList);
		}
		return $languageList;
	}
	
	/**
	 * 获取语言详细信息
	 * @param int $language_id 语言ID
	 */
	public static function getLanguageInfo($language_id)
	{
		$languageList = self::getLanguageList();
		return $languageList[$language_id] ? $languageList[$language_id] : false; 
	}
	
	/**
	 * 获取所有地区列表
	 */
	public static function getRegionList()
	{
		$regionList = array();
		if(S('region_list')){
			$regionList = S('region_list');
		}else{
			$region_db = D('region');
			$regionList = $region_db->field('id,name,iso2_code,iso3_code,country_number,pid')->where('status=1')->order('id ASC')->select();
			S('region_list', $regionList);
		}
		return $regionList;
	}
	
	/**
	 * 获取地区信息
	 * @param int $id
	 */
	public static function getRegionInfo($id)
	{
		$region_db = D('region');
		$regionInfo = $region_db->find($id);
		return $regionInfo;
	}
	
	/**
	 * 获取国家列表
	 */
	public static function getCountryList()
	{
		$countryList = array();
		if(S('country_list')){
			$countryList = S('country_list');
		}else{
			$region_db = D('region');
			$countryList = $region_db->field('id,name,iso2_code,iso3_code,country_number')->where('pid=0 AND status=1')->order('name ASC')->select();
//			foreach ($result as $v){
//				$countryList[$v['id']] = $v;
//			}
			S('country_list', $countryList);
		}
		return $countryList;
	}
	
	/**
	 * 获取省,市列表
	 * @param int $pid 上级ID号
	 * @return ArrayObject
	 */
	public static function getAreaList($pid = 0)
	{
		$areaArray = array();
		$pid = intval($pid);
		$region_db = D('region');
		$areaArray = $region_db->field('id,name')->where('pid='.$pid.' AND status=1')->select();
		return $areaArray;
	}
	
	/**
	 * 更新邮件模板
	 * @param array $post	post数据
	 * @param int $tpl_id 模板ID
	 */
	public static function updateEmailTemplate($post, $tpl_id = null)
	{
		$data = array(
			'name' => isset($post['name']) ? trim($post['name']) : null,
			'variable' => isset($post['variable']) ? trim($post['variable']) : null,
		);
		
		$tpldb = D('email_template');
		if(intval($tpl_id)){
			$tpldb->where('id='.intval($tpl_id))->save($data);
		}else{
			$data['path_alias'] = isset($post['path_alias']) ? $post['path_alias'] : null;
			$tpl_id = $tpldb->add($data);
		}
		return $tpl_id;
	}
	
	/**
	 * 更新邮件模板详细内容
	 * @param array $post	post数据
	 * @param int $tpl_id 模板ID
	 */
	public static function updateEmailTemplateItems($post, $tpl_id)
	{
		if(!intval($tpl_id)) return false;
		$tpli_db = D('email_template_items');
		//清空原数据
		$tpli_db->where(array('email_template_id' => $tpl_id))->delete();
		//语言列表
		$languageList = SiteModel::getLanguageList();
		foreach($languageList as $language_id => $language_name){
			if($post[$language_id]['title']){
				$data = array(
					'email_template_id' => $tpl_id,
					'language_id' => $language_id,
					'title' => trim($post[$language_id]['title']),
					'content' => trim($post[$language_id]['content']),
				);
				$tpli_db->add($data);
			}
		}
	}
	
	/**
	 * 获取邮件模板信息
	 * @param int $tpl_id 模板ID
	 * @param int $language_id 语言ID
	 */
	public static function getEmailTemplateItems($tpl_id, $language_id = null)
	{
		$tpli_db = D('email_template_items');
		$where['email_template_id'] = $tpl_id;
		if($language_id){
			$where['language_id'] = $language_id;
		}
	
		$emailArray = array();
		$items = $tpli_db->where($where)->select();
		if($items){
			foreach ($items as $is){
				$emailArray[$is['language_id']] = $is;
			}
		}
		return $emailArray ? $emailArray : false;
	}
	
	/**
   * 前台获取货币列表
   * @return array
   */
	public static function currencyList()
	{
		$defaultCurrency = C('currency');
		$currencyList = $currencyArray = array();
		if(S('currency_list')){
			$currencyList = S('currency_list');
		}else{
			$currency_db = D('currency');
			$currencyList = $currency_db->where('visible=1')->select();
			S('currency_list', $currencyList);
		}
		foreach ($currencyList as $curr)
		{
			$curr['isdefault'] = ($defaultCurrency == $curr['name']);
			$currencyArray[$curr['name']] = $curr;
		}
		return $currencyArray;
	}
	
	/**
	 * 获取站点配置数据
	 */
	public static function getShopConfigList()
	{
		$configList = array();
		if(S('shop_config_list')){
			$configList = S('shop_config_list');
		}else{
			$config_db = D('shop_config');
			$configArray = $config_db->field('config_name,config_value')->select();
			if($configArray){
				foreach ($configArray as $con){
					$configList[$con['config_name']] = $con['config_value'];
				}
			}
			S('shop_config_list', $configList);
		}
		return $configList;
	}
	
	/**
	 * 更新站点配置信息
	 * @param array $config_array
	 */
	public static function updateShopConfig($config_array)
	{
		if(!is_array($config_array)) return false;
		$configList = self::getShopConfigList();
		$config_db = D('shop_config');
		foreach ($config_array as $config_name => $config_value){
			if(array_key_exists($config_name, $configList)){
				$config_db->where(array('config_name' => $config_name))->save(array('config_value' => $config_value));
			}else{
				$config_db->add(array('config_name' => $config_name, 'config_value' => $config_value));
			}
		}
		S('shop_config_list', null);
	}
}